from sanic.response import json as json_res
from util.main import jsonify
from ph_utils.logger import log


async def error_hanlder(request, exception):
    """异常处理函数, 用于处理全局异常

    Args:
        request (sanic.Request): 请求对象

    Returns:
        sanic.Response: 返回对象
    """
    status = 500000
    message = "系统异常"
    if hasattr(exception, "status_code"):
        status = exception.status_code
        message = exception.message
    log(name="sanic.error", exception=exception)
    return json_res(jsonify(code=status, message=message))
